package com.dc.controller;

import com.dc.base.aop.AopOperation;
import com.dc.base.baseController.BaseController;
import com.dc.base.em.ErrorMessageEnum;
import com.dc.base.em.RoleMenuEnum;
import com.dc.base.vo.BaseModel;
import com.dc.base.vo.BusinessException;
import com.dc.model.Rent;
import com.dc.model.User;
import com.dc.service.RentService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

@Api(value="购入模块")
@RequestMapping("/rent")
@Controller
public class RentController extends BaseController {
    @Autowired
    RentService rentService;

    // 分页查询购入信息
    // 新增日志信息
    @AopOperation(desc = "分页查询购入信息",type = "查询",menu = RoleMenuEnum.NO_201)
    @ApiOperation(value="分页查询购入信息",httpMethod = "GET",notes = "查询")
    @RequestMapping(value = "/selectPageRent", method = RequestMethod.GET)
    @ResponseBody
    public BaseModel selectPageRent(
            @RequestParam(value="curr_page",defaultValue = "1") int curr_page,
            @RequestParam(value="page_size",defaultValue = "10") int page_size,
            BaseModel baseModel,
            @RequestParam(value="rent_id",required = false) Integer rent_id,
            @RequestParam(value="client_name",required = false)String client_name,
            @RequestParam(value="flower_name",required = false)String flower_name){
        return  rentService.selectPageRent(curr_page,page_size,baseModel,rent_id,client_name,flower_name);
    }

    // 操作日志注解，新增操作日志
    @AopOperation(desc = "新增购入",type="新增",menu = RoleMenuEnum.NO_201)
    // 新增购入信息
    @ApiOperation(value="购入新增",httpMethod = "POST",notes="新增")
    @RequestMapping(value="/addRent", method = RequestMethod.POST)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(value="到期时间",name="expiration_dates",required = true,dataType = "String",paramType = "query")
    })
    public BaseModel addRent(Rent rent, HttpServletRequest request, BaseModel baseModel)throws Exception{
        rent.setCreate_time(new Date());
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        Date date = simpleDateFormat.parse(expiration_dates);
        rent.setRent_time(new Date());
        /* 获取session,判断是否登录 */
        HttpSession session=request.getSession();
        User currUser=(User)session.getAttribute("currentUser");
        if(currUser==null) {
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return rentService.addRent(rent,baseModel);
    }

    // 操作日志注解，新增操作日志
    @AopOperation(desc = "修改购入信息",type="修改",menu = RoleMenuEnum.NO_201)
    // 修改购入信息
    @ApiOperation(value="购入修改",httpMethod = "POST",notes="修改")
    @RequestMapping(value="/updateRent", method = RequestMethod.POST)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(value="到期时间",name="expiration_dates",required = true,dataType = "String",paramType = "query")
    })
    public BaseModel updateRent(Rent rent,BaseModel baseModel,HttpServletRequest request)throws Exception {
        rent.setUpdate_time(new Date());
//        if(expiration_dates!=null){
//            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//            Date date = simpleDateFormat.parse(expiration_dates);
//            rent.setRent_time(date);
//        }
        rent.setRent_time(new Date());
        // 获取登录信息
        HttpSession session=request.getSession();
        // 记住要用User
        User currentUser=(User) session.getAttribute("currentUser");
        if(currentUser==null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return rentService.updateRent(rent,baseModel);
    }

    // 根据id查询购入信息
    @ApiOperation(value="购入查询",httpMethod = "GET",notes="查询")
    @RequestMapping(value="/selectRentById", method = RequestMethod.GET)
    @ResponseBody
    public BaseModel selectRentById(@RequestParam("rent_id")Integer rent_id,
                                      BaseModel baseModel,
                                      HttpServletRequest request)throws Exception{
        return rentService.selectRentById(rent_id,baseModel);
    }

    // 操作日志注解，新增操作日志
    @AopOperation(desc = "删除购入",type="删除",menu = RoleMenuEnum.NO_201)
    // 删除购入信息(修改)
    @ApiOperation(value="购入删除",httpMethod = "POST",notes="删除")
    @RequestMapping(value="/deleteRent/{ids}", method = RequestMethod.POST)
    @ResponseBody
    public BaseModel deleteRent(@PathVariable(value="ids") String ids,
                                  BaseModel baseModel,
                                  HttpServletRequest request)throws Exception{
        HttpSession session=request.getSession();
        User currentUser = (User)session.getAttribute("currentUser");
        if(currentUser==null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return rentService.deleteRent(ids,baseModel);
    }
}
